/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ihale.satinalmaci;

import java.io.IOException;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import org.ihale.database.Database;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.ihale.Ihale;
import org.ihale.database.DatabaseEmre;
import org.ihale.tedarikci.Tedarikci;

/**
 *
 * @author yakup
 */
@ManagedBean(name = "satinalmacibean")
@SessionScoped
public class SatinAlmaciBean {

    private SatinAlmaci satinAlmaci;
    private String satinalmaci_id;
    private SatinAlmaci getirilenSatinAlmaci;
    private SatinAlmaci secilenSatinalmaci;
    private List<SatinAlmaci> satinAlmaciListesi;
    private List<Ihale> listeIhale = new ArrayList<Ihale>();
    private Ihale secilenIhaleToGoruntule = new Ihale();
    private List<Tedarikci> listeTedarikciToIhaleGoruntule = new ArrayList<Tedarikci>();

    public SatinAlmaciBean() {
        this.satinAlmaci = new SatinAlmaci();
        this.satinAlmaciListesi = new ArrayList<SatinAlmaci>();
        this.secilenSatinalmaci = new SatinAlmaci();
        Database database = new Database();
        try {
            database.connect();
            this.satinAlmaciListesi = database.listeSatinAlmaci();
            database.disconnect();
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Liste veri tabanından alınamadı!"));
            System.out.println(e.toString());
        }


    }

    public void listeIhaleGecmis() {
    }

    public void listeIhaleSimdiki() throws ClassNotFoundException, SQLException, IOException {
        DatabaseEmre db = new DatabaseEmre();
        db.connect();
        listeIhale = db.listeleIhaleSimdiki();

        db.disconnect();
        FacesContext.getCurrentInstance().getExternalContext().redirect("SatinAlmaciIhaleleriGoruntule.xhtml");
    }

    public void listeIhaleGelecek() {
    }

    public void satinAlmaciIhaleDetay() {
        DatabaseEmre db = new DatabaseEmre();
        try {
            db.connect();
            listeTedarikciToIhaleGoruntule = db.listeTedarikciToIhale(secilenIhaleToGoruntule.getId());
            db.disconnect();
            FacesContext.getCurrentInstance().getExternalContext().redirect("SatinAlmaciIhaleDetay.xhtml");
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kaldırma işlemi başarısız."));
            System.out.println(e.toString());
        }
    }

    public void ekleSatinalmaci() {
        Database database = new Database();
        try {
            database.connect();
            if (database.ekleSatinAlmaci(this.satinAlmaci)) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklendi."));
                this.satinAlmaciListesi = database.listeSatinAlmaci();
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklenemedi."));
            }

            database.disconnect();

        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kaldırma işlemi başarısız."));
            System.out.println(e.toString());
        }
    }

    public void getirSatinAlmaci() {
        Database database = new Database();

        try {
            database.connect();
            this.getirilenSatinAlmaci = database.getirSatinAlmaci(this.satinalmaci_id);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklendi."));

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklenemedi."));

            database.disconnect();

        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kaldırma işlemi başarısız."));
            System.out.println(e.toString());
        }

    }

    public void silSatinAlmaci() {
        Database database = new Database();

        try {
            database.connect();
            if (database.silSatinAlmaci(this.secilenSatinalmaci)) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı silindi."));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Silinemedi!!!."));
            }

            this.satinAlmaciListesi = database.listeSatinAlmaci();

            database.disconnect();
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Silinemedi!!!."));
            System.out.println(e.toString());
        }


    }

    public void guncelleSatinAlmaci() {
        Database database = new Database();

        try {
            database.connect();
            if (database.guncelleSatinAlmaci(this.secilenSatinalmaci)) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı güncellendi!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı güncellenemedi!"));
            }

            this.satinAlmaciListesi = database.listeSatinAlmaci();

            database.disconnect();

        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Güncellenemedi!!!."));
            System.out.println(e.toString());
        }

    }

    public SatinAlmaci getSecilenSatinalmaci() {
        return secilenSatinalmaci;
    }

    public void setSecilenSatinalmaci(SatinAlmaci secilenSatinalmaci) {
        this.secilenSatinalmaci = secilenSatinalmaci;
    }

    public SatinAlmaci getGetirilenSatinAlmaci() {
        return getirilenSatinAlmaci;
    }

    public void setGetirilenSatinAlmaci(SatinAlmaci getirilenSatinAlmaci) {
        this.getirilenSatinAlmaci = getirilenSatinAlmaci;
    }

    public String getSatinalmaci_id() {
        return satinalmaci_id;
    }

    public void setSatinalmaci_id(String satinalmaci_id) {
        this.satinalmaci_id = satinalmaci_id;
    }

    public List<SatinAlmaci> getSatinAlmaciListesi() {
        return satinAlmaciListesi;
    }

    public SatinAlmaci getSatinAlmaci() {
        return satinAlmaci;
    }

    public void setSatinAlmaci(SatinAlmaci satinAlmaci) {
        this.satinAlmaci = satinAlmaci;
    }

    public SatinAlmaciBean(SatinAlmaci satinAlmaci) {
        this.satinAlmaci = satinAlmaci;

    }

    public List<Ihale> getListeIhale() {
        return listeIhale;
    }

    public void setListeIhale(List<Ihale> listeIhale) {
        this.listeIhale = listeIhale;
    }

    public List<Tedarikci> getListeTedarikciToIhaleGoruntule() {
        return listeTedarikciToIhaleGoruntule;
    }

    public void setListeTedarikciToIhaleGoruntule(List<Tedarikci> listeTedarikciToIhaleGoruntule) {
        this.listeTedarikciToIhaleGoruntule = listeTedarikciToIhaleGoruntule;
    }

    public Ihale getSecilenIhaleToGoruntule() {
        return secilenIhaleToGoruntule;
    }

    public void setSecilenIhaleToGoruntule(Ihale secilenIhaleToGoruntule) {
        this.secilenIhaleToGoruntule = secilenIhaleToGoruntule;
    }
}
